package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.model.domain.Area;
import com.example.mybatisplus.mapper.AreaMapper;
import com.example.mybatisplus.model.domain.Dot;
import com.example.mybatisplus.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzy
 * @since 2024-06-24
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
    public List<Area> selectAreaPage(Dot dot){
        return areaMapper.selectAreaPage(dot);
    }
    public List<Area> selectAreaPageByName(Dot dot){
        return areaMapper.selectAreaPageByName(dot);
    }


    public int get_total_people_by_AreaID(long AreaID) {
        return areaMapper.getTotelPeople(AreaID);
    }


    public boolean areaModify(Area area) {
        QueryWrapper<Area> wrapper = new QueryWrapper<Area>().eq("ID",area.getId()).eq("is_deleted", area.getIsDeleted());
        Area area1 = areaMapper.selectOne(wrapper);
        if(area1!=null){
            areaMapper.updateById(area);
            return true;
        }
        return false;


    }

    @Override
    public boolean areaDelete(Long areaID) {
        Area area = new Area().setId(areaID).setIsDeleted(1).setModifyTime(LocalDateTime.now());
        areaMapper.updateById(area);
        return true;
    }

    @Override
    public boolean areaAdd(Map<String, Object> params) {


        List<Area> areaList = areaMapper.selectByMap(params);
        if(areaList.isEmpty()){
            Area area = new Area()
                    .setIsDeleted(0)
                    .setName((String)params.get("name"))
                    .setAddress((String)params.get("address"))
                    .setCreatedTime(LocalDateTime.now())
                    .setModifyTime(LocalDateTime.now());

            areaMapper.insert(area);
            return true;
        }
        for(Area area:areaList){
            if(area.getIsDeleted()==1){
                area.setIsDeleted(0);
                areaMapper.updateById(area);
                return true;
            }
            else{
                return false;
            }
        }
        return false;


    }

    @Override
    public List<Area> selectAreaByBuildingIDs(List<Long> ids) {
        return areaMapper.selectAreaByBuildingIDs(ids);
    }
}
